// pages/home/home.js
const db=wx.cloud.database()
let searchKey=""
const app=getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        banner: [{
            picUrl: '../../images/banner_10.jpg'
          },
          {
            picUrl: '../../images/banner_11.jpg'
          }, {
            picUrl: '../../images/banner_13.jpg'
          }
        ],
        cycleText:"woaini",
        tabs:[],
        projectList:[],
        searchKey:"",
        tabbar: {},
        address:{},//地址
        village:'',
        showarea:false,//是否显示地址器
        strictId:["8f75309d629b552607022fa76f826c31","058dfefe629b55ba0732cde60174d3a7","0a4ec1f9629b5670089ed9392e1f462e"],//朴实严谨的项目id
        strictProjectList:[]
    },
    getCycleText(){
         db.collection('cycleText')
         .get()
         .then(res=>{
           console.log("res",res)
           var i=0
           setInterval(() => {
            let cycleTextItem=res.data[i].text
            i=i+1
            if(i==3)
            i=0
            this.setData({
              cycleText:cycleTextItem
            })
        }, 5000)
              
     })
    },
    // getProject()
    // {
    //   db.collection('project').aggregate()
    //   .group({
    //       _id:'$taskType'
    //   })
    //   .end()
    //   .then(res=>{
    //     console.log("项目类型",res.list)
    //     this.setData({
    //       tabs:res.list
    //     })
    //   })
    // },
    // getProject001(){
    //  db.collection('project')
    //  .get()
    //  .then(res=>{
    //    console.log(res)
    //    this.setData({
    //     projectList:res.data
    //    })
    //  })
    // },
    getSearchKey(e){
       console.log(e.detail)
       searchKey=e.detail
    },
    goSearch(){
        wx.navigateTo({
          url: '../../pages/search/search?searchKey='+searchKey,
        })
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad:function (options) {
     
      console.log("home页",app.globalData.userInfo)
      wx.hideTabBar()
      app.editTabbar()
      this.getCycleText()
      this.confirmAddress();
      this.getStrictProject()
      //this.getProject()
      //this.getProject001()
    },
    confirmAddress(){
      const userInfo=app.globalData.userInfo.userInfo;
      if(userInfo){
        if(userInfo.regionDetailObject){
          this.setData({
            village:userInfo.regionDetailObject.village,
          })
        } 
      }
    },
    goPlant(e){
      console.log(e)
       wx.navigateTo({
         url: '../plant/plant?id='+e.currentTarget.dataset.id
       })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      app.editTabbar()
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
      app.editTabbar()
    },
    //打开地址选择器
    openAddress(){
      this.setData({
        showarea:true,
        address:this.data.address
      })
    },
    getStrictProject(){
      for (let index = 0; index < 3; index++) {
        db.collection("project").doc(this.data.strictId[index]).get()
        .then(res=>{
          console.log("严谨的项目列表获取成功",res.data)
          this.data.strictProjectList.push(res.data)
          console.log(this.data.strictProjectList)
          this.setData({
            strictProjectList:this.data.strictProjectList
          })
        })
      }
    
    },
    /*地址选择器监听事件：当所选地址与先前地址不一致时触发，参数e.detail.address为重选后的五级地址对象*/
    async changeAddress(e){
      console.log('所选地址与先前地址不一致',e.detail);
      let address = e.detail.address;
      this.setData({
        address:address,
        village:address.village,
      })
      let addressString = address.province+address.city+address.country+address.street+address.village;
      await wx.cloud.callFunction({
          name:"updateUserInfo",
          data:{
            action:"userInfo",
            openid:app.globalData.openid,
            regionDetail: addressString,
            regionDetailObject:address
          }
      })
      .then(res=>{
          console.log("更新志愿者村庄成功")
      })
      
    }
})